//顶部导航登录/////////
$('#signin_submit').live('click', function() {
	var username = $("#username:visible").val();
	var email = $("#email:visible").val();
	var password = $("#password").val();
	var is_remember = ($("#remember:checked").val()) ? true : false;
	
	
	if (username == document.getElementById("username").defaultValue) {
		$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入用户名</span>");
		return;
	}
	if (email == document.getElementById("email").defaultValue) {
		$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入电子邮箱</span>");
		return;
	}
	if (password == "") {
		$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入密码</span>");
		return;
	}
	
	if (username) {
		$("#signin_menu").spin();
		$.ajax({
			type: 'POST',
			url: "/index.php/frontend/ajax_login/index",
			data: {username: username, password: password, is_remember: is_remember},
			dataType: 'json',
			success: function(data){
				$("#signin_menu").spin(false);
				switch (data)
				{
				case 'null_username_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入用户名</span>");
					break;
				case 'null_password_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入密码</span>");
					break;
				case 'name_length_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名长度应为5~16字节</span>");
					break;
				case 'name_pattern_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名格式错误<br />（字母开头，允许字母数字下划线）</span>");
					break;
				case 'password_length_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录密码长度应为4~10字节</span>");
					break;
				case 'username_error':
					$('#login_msg').html("<span class='tip error'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;该用户不存在</span>");
					break;
				case 'password_error':
					$('#login_msg').html("<span class='tip error'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录密码错误</span>");
					break;
				case 'succeed':
					var url = location.href;
					if(url.indexOf("login") > 0 || url.indexOf("register") > 0) {
						$('#login_msg').html("<span class='tip smile'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登陆成功，正在转向管理页面...</span>");
						location.replace("/index.php/backend/" + data + "/index");
					} else {
						$('#login_msg').html("<span class='tip smile'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登陆成功，正在转向登陆前页面...</span>");
						location.reload();
					}
					break;
				}
			}
		});
	} else if (email) {
		$("#signin_menu").spin();
		$.ajax({
			type: 'POST',
			url: "/index.php/frontend/ajax_login/index",
			data: {email: email, password: password, is_remember: is_remember},
			dataType: 'json',
			success: function(data){
				$("#signin_menu").spin(false);
				switch (data)
				{
				case 'null_email_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入电子邮箱</span>");
					break;
				case 'null_password_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请输入密码</span>");
					break;
				case 'email_pattern_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;电子邮箱格式错误</span>");
					break;
				case 'password_length_error':
					$('#login_msg').html("<span class='tip attention'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录密码长度应为4~10字节</span>");
					break;
				case 'email_error':
					$('#login_msg').html("<span class='tip error'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;该电子邮箱不存在</span>");
					break;
				case 'password_error':
					$('#login_msg').html("<span class='tip error'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录密码错误</span>");
					break;
				case 'succeed':
					var url = location.href;
					if(url.indexOf("login") > 0 || url.indexOf("register") > 0) {
						$('#login_msg').html("<span class='tip smile'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登陆成功，正在转向管理页面...</span>");
						location.replace("/index.php/backend/" + data + "/index");
					} else {
						$('#login_msg').html("<span class='tip smile'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登陆成功，正在转向登陆前页面...</span>");
						location.reload();
					}
					break;
				}
			}
		});
	}
});